<?php
/************************************************************************
Class UserType
----------------------------------------------------------------
PICKCOCMS Project
Company: PICKCO  Co., Ltd 
Name: Nguyen Hai Nam                                  
Last updated: 29/10/2009
**************************************************************************/
class Pick_Core_Actions{
	var $action;
	var $request;
	var $messages;
	var $infoaction; // Thong tin cua hanh dong tra ve
	function Pick_Core_Actions() {

	}
	/* Tu dong nhan ra hanh dong cua doi tuong va chon phuong thuc thich hop */
	function auto($plus, $condition = '1=1'){
		switch ($plus) {	
			case 'deleteItem'		: $this->infoaction = $this->deleteItem(); break;
			case 'deleteItems'		: $this->infoaction = $this->deleteItems(); break;
			case 'activateItem'		: $this->infoaction = $this->activateItem(); break;
			case 'activateItems'	: $this->infoaction = $this->activateItems(); break;
			case 'disableItem'		: $this->infoaction = $this->disableItem(); break;
			case 'disableItems' 	: $this->infoaction = $this->disableItems(); break;
			case 'setHome' 			: $this->infoaction = $this->setHome(); break;
			case 'deletesetHome' 	: $this->infoaction = $this->deletesetHome(); break;
			case 'up'				: $this->infoaction = $this->up(); break;
			case 'down'				: $this->infoaction = $this->down(); break;
			case 'changePosition'	: $this->infoaction = $this->changePosition(); break;
			case 'changePositions'	: $this->infoaction = $this->changePosition(); break;
			case 'cleanTrash'		: $this->infoaction = $this->cleanTrash($condition); break;
			default					: $this->infoaction = '';
		}
		return;
	}
	/* Set field for object */
	function setInfoAction($object = '', $req = '', $ames = ''){
		global $request, $messages;
		$this->action  		= $object;
		$this->request 		= $req!=''?$req:$request;
		$this->messages 	= $ames!=''?$ames:$messages;
	}	
	/* Lay thong tin action sau khi function auto duoc thuc hien */
	function getInfoAction(){
		return 	$this->infoaction;
	}
	/*  Xoa doi tuong */
	function deleteItem(){
		$oId = $this->request->element('oId');
		if($oId != '') {
			$this->action->update(array('status' => 2), "id = '$oId'");
			return l('item_delete_ok');
		}
		return l('delete_error_no_item');
	}
	/* Xoa nhieu doi tuong */
	function deleteItems(){
		$oIds = $this->request->element('oIds');
		if($oIds != '') {
			$this->action->update(array('status' => 2), "id IN (".join(',',$oIds).")");
			return l('item_delete_ok');
		}
		return l('delete_error_no_item');
	}
	/* Active mot doi tuong */
	function activateItem(){
		$oId = $this->request->element('oId');
		if($oId != '') {
			$this->action->update(array('status' => 1), "id = '$oId'");
			return l('item_activate_ok');
		}
		return l('activate_error_no_item');
	}
	/* Active nhieu doi tuong */
	function activateItems(){
		$oIds = $this->request->element('oIds');
		if($oIds != '') {		
			$this->action->update(array('status' => 1), "id IN (".join(',',$oIds).")");
			return l('item_activate_ok');
		}
		return l('activate_error_no_item');
	}
	/* Vo hieu hoa mot doi tuong */
	function disableItem(){
		$oId = $this->request->element('oId');
		if($oId != '') {
			$this->action->update(array('status' => 0), "id = '$oId'");
			return l('item_disable_ok');
		}
		return l('disable_error_no_item');
	}
	/* Vo hieu hoa nhieu doi tuong */
	function disableItems(){
		$oIds = $this->request->element('oIds');
		if($oIds != '') {		
			$this->action->update(array('status' => 0), "id IN (".join(',',$oIds).")");
			return l('item_disable_ok');
		}
		return l('disable_error_no_item');
	}
	/* Đưa đối tượng lên trang chủ */
	function setHome(){
		$oId = $this->request->element('oId', '');
		if($oId != '') {		
			$this->action->update(array('home' => 1), "id = '$oId'");
			return l('item_sethome_ok');
		}
		return l('disable_error_no_item');
	}
	/* Xóa đối tưởng khỏi trang chủ */
	function deletesetHome(){
		$oId = $this->request->element('oId', '');
		if($oId != '') {		
			$this->action->update(array('home' => 0), "id = '$oId'");
			return l('item_deletesethome_ok');
		}
		return l('disable_error_no_item');
	}
	/* Up */
	function up(){
		$oId 		= $this->request->element('oId', '');
		$position 	= $this->request->element('position');
		if($this->action->up($oId, $position))
			return l('item_up_ok');
		return l('item_up_error');
	}
	/* Down */
	function down(){
		$oId 		= $this->request->element('oId');
		$position 	= $this->request->element('position');
		if($this->action->down($oId, $position))
			return l('item_down_ok');
		return l('item_down_error');
	}
	/* changePosition */
	function changePosition(){
		$oId 		= $this->request->element('oId');
		$position 	= $this->request->element('position');
		if($this->action->changePosition($oId, $position))
			return l('item_change_position_ok');
		return l('item_change_position_error');
	}
	/* cleanTrash */
	function cleanTrash($condition = "1=1"){
		$this->action->cleanTrash($condition . " AND status = '2'");
		return l('clean_trash_ok');
	}
}
?>